﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Transactions;

public partial class Main_Inventory_IssueItem : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    DataTable dtDetail = new DataTable();
    int tblCounter = 0;
    List<string> LstID = new List<string>();
    List<string> LstItemCode = new List<string>();
    List<string> LstRegid = new List<string>();
    List<string> LstItemDesc = new List<string>();
    //List<string> LstPeCode = new List<string>();
    //List<string> LstProductDesc = new List<string>();
    List<string> LstRO = new List<string>();
    List<string> LstCustomer = new List<string>();
    List<string> LstProjectName = new List<string>();
    List<string> LstCustomerID = new List<string>();
    List<string> LstProjectID = new List<string>();
    List<string> LstLeasedType = new List<string>();
    List<string> LstAvailQty = new List<string>();
    List<string> LstItemQty = new List<string>();
    List<string> LstRequestQty = new List<string>();
    List<string> LstIssueQty = new List<string>();
    List<string> LstItemid = new List<string>();
    List<string> LstTitid = new List<string>();
    List<string> LstBalanceQTY = new List<string>();
    List<string> LstIssuedQty = new List<string>();
    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        ViewstateControl();
    }
    private void ViewstateControl()
    {
        LstID = (List<string>)ViewState["LstID"];
        LstItemCode = (List<string>)ViewState["LstItemCode"];
        LstItemDesc = (List<string>)ViewState["LstItemDesc"];
        //LstPeCode = (List<string>)ViewState["LstPeCode"];
        //LstProductDesc = (List<string>)ViewState["LstProductDesc"];
        LstRO = (List<string>)ViewState["LstRO"];
        LstCustomer = (List<string>)ViewState["LstCustomer"];
        LstProjectName = (List<string>)ViewState["LstProjectName"];
        LstCustomerID = (List<string>)ViewState["LstCustomerID"];
        LstProjectID = (List<string>)ViewState["LstProjectID"];
        //LstLeasedType = (List<string>)ViewState["LstLeasedType"];
        LstRequestQty = (List<string>)ViewState["LstRequestQty"];
        LstAvailQty = (List<string>)ViewState["LstAvailQty"];
        LstIssueQty = (List<string>)ViewState["LstIssueQty"];
        LstBalanceQTY = (List<string>)ViewState["LstBalanceQTY"];
        LstItemid = (List<string>)ViewState["LstItemid"];
        LstTitid = (List<string>)ViewState["LstTitid"];
        LstRegid = (List<string>)ViewState["LstRegid"];
        LstItemQty = (List<string>)ViewState["LstItemQty"];
        LstIssuedQty = (List<string>)ViewState["LstIssuedQty"];
        if (Session["hidCounter"] != null)
        {
            int count = Convert.ToInt32(Session["hidCounter"].ToString());
            for (int i = 0; i < count; i++)
            {
                HtmlTableRow htRow = new HtmlTableRow();

                HtmlTableCell htcell1 = new HtmlTableCell();
                TextBox txtItemCode = new TextBox();
                txtItemCode.ID = LstItemCode[i].ToString();
                txtItemCode.Width = Unit.Percentage(99);
                txtItemCode.CssClass = "input";
                txtItemCode.ForeColor = System.Drawing.Color.Black;
                txtItemCode.Enabled = false;
                htcell1.Controls.Add(txtItemCode);
                HiddenField Hid = new HiddenField();
                Hid.ID = LstID[i].ToString();
                htcell1.Controls.Add(Hid);

                HtmlTableCell htcell2 = new HtmlTableCell();
                TextBox txtItemDesc = new TextBox();
                txtItemDesc.ID = LstItemDesc[i].ToString();
                txtItemDesc.Width = Unit.Percentage(99);
                txtItemDesc.CssClass = "input";
                txtItemDesc.ForeColor = System.Drawing.Color.Black;
                txtItemDesc.Enabled = false;
                htcell2.Controls.Add(txtItemDesc);

                //HtmlTableCell htcell3 = new HtmlTableCell();
                //TextBox txtpecode = new TextBox();
                //txtpecode.ID = LstPeCode[i].ToString();
                //txtpecode.Width = Unit.Percentage(99);
                //txtpecode.ForeColor = System.Drawing.Color.Black;
                //txtpecode.Enabled = false;
                //txtpecode.CssClass = "labelText";
                //htcell3.Controls.Add(txtpecode);

                //HtmlTableCell htcell4 = new HtmlTableCell();
                //TextBox txtProductDesc = new TextBox();
                //txtProductDesc.ID = LstProductDesc[i].ToString();
                //txtProductDesc.Width = Unit.Percentage(99);
                //txtProductDesc.ForeColor = System.Drawing.Color.Black;
                //txtProductDesc.Enabled = false;
                //txtProductDesc.CssClass = "labelText";
                //htcell4.Controls.Add(txtProductDesc);

                HtmlTableCell htcell5 = new HtmlTableCell();
                TextBox txtRO = new TextBox();
                txtRO.ID = LstRO[i].ToString();
                txtRO.Width = Unit.Percentage(99);
                txtRO.ForeColor = System.Drawing.Color.Black;
                txtRO.Enabled = false;
                txtRO.CssClass = "input";
                htcell5.Controls.Add(txtRO);

                HtmlTableCell htcell6 = new HtmlTableCell();
                TextBox txtCustomer = new TextBox();
                txtCustomer.ID = LstCustomer[i].ToString();
                txtCustomer.Width = Unit.Percentage(99);
                txtCustomer.ForeColor = System.Drawing.Color.Black;
                txtCustomer.Enabled = false;
                txtCustomer.CssClass = "input";
                htcell6.Controls.Add(txtCustomer);
                HiddenField hidCustomer = new HiddenField();
                hidCustomer.ID = LstCustomerID[i].ToString();
                htcell6.Controls.Add(hidCustomer);


                HtmlTableCell htcell7 = new HtmlTableCell();
                TextBox txtProjectname = new TextBox();
                txtProjectname.ID = LstProjectName[i].ToString();
                txtProjectname.Width = Unit.Percentage(99);
                txtProjectname.ForeColor = System.Drawing.Color.Black;
                txtProjectname.CssClass = "input";
                txtProjectname.Enabled = false;
                htcell7.Controls.Add(txtProjectname);
                HiddenField hidProject = new HiddenField();
                hidProject.ID = LstProjectID[i].ToString();
                htcell7.Controls.Add(hidProject);


                HtmlTableCell htcell8 = new HtmlTableCell();
                TextBox txtAvailableqty = new TextBox();
                txtAvailableqty.ID = LstAvailQty[i].ToString();
                txtAvailableqty.Width = Unit.Percentage(99);
                txtAvailableqty.Enabled = false;
                txtAvailableqty.ForeColor = System.Drawing.Color.Black;
                txtAvailableqty.CssClass = "inputnumber";
                htcell8.Controls.Add(txtAvailableqty);

                HtmlTableCell htcell9 = new HtmlTableCell();
                TextBox lblrequestqty = new TextBox();
                lblrequestqty.ID = LstRequestQty[i].ToString();
                lblrequestqty.Width = Unit.Percentage(99);
                lblrequestqty.Enabled = false;
                lblrequestqty.ForeColor = System.Drawing.Color.Black;
                lblrequestqty.CssClass = "inputnumber";
                htcell9.Controls.Add(lblrequestqty);

                HtmlTableCell htcell10 = new HtmlTableCell();
                TextBox txtIssueQty = new TextBox();
                txtIssueQty.ID = LstIssueQty[i].ToString();
                txtIssueQty.Width = Unit.Percentage(99);
                txtIssueQty.CssClass = "inputnumber";
                //txtIssueQty.Attributes.Add("onblur", "return BalanceQty(" + tblCounter.ToString() + ");");
                txtIssueQty.Attributes.Add("onblur", "return CheckValue(this.id);");
                txtIssueQty.Attributes.Add("onKeyPress", "return isNumber(event);");
                htcell10.Controls.Add(txtIssueQty);

                HtmlTableCell htcell11 = new HtmlTableCell();
                HiddenField hidItemid = new HiddenField();
                hidItemid.ID = LstItemid[i].ToString();
                htcell11.Controls.Add(hidItemid);

                HtmlTableCell htcell12 = new HtmlTableCell();
                HiddenField hidTITID = new HiddenField();
                hidTITID.ID = LstTitid[i].ToString();
                htcell12.Controls.Add(hidTITID);

                HtmlTableCell htcell13 = new HtmlTableCell();
                TextBox txtBalanceQty = new TextBox();
                txtBalanceQty.ID = LstBalanceQTY[i].ToString();
                txtBalanceQty.Width = Unit.Percentage(99);
                txtBalanceQty.CssClass = "inputnumber";
                txtBalanceQty.Enabled = false;
                txtBalanceQty.ForeColor = System.Drawing.Color.Black;
                htcell13.Controls.Add(txtBalanceQty);

                HtmlTableCell htcell14 = new HtmlTableCell();
                HiddenField hidregid = new HiddenField();
                hidregid.ID = LstRegid[i].ToString();
                htcell14.Controls.Add(hidregid);

                HtmlTableCell htcell15 = new HtmlTableCell();
                HiddenField hiditemqty = new HiddenField();
                hiditemqty.ID = LstItemQty[i].ToString();
                htcell15.Controls.Add(hiditemqty);

                HtmlTableCell htcell16 = new HtmlTableCell();
                TextBox txtIssuedQty = new TextBox();
                txtIssuedQty.ID = LstIssuedQty[i].ToString();
                txtIssuedQty.Width = Unit.Percentage(99);
                txtIssuedQty.CssClass = "inputnumber";
                txtIssuedQty.Enabled = false;
                txtIssuedQty.ForeColor = System.Drawing.Color.Black;
                htcell16.Controls.Add(txtIssuedQty);

                //HtmlTableCell htcell17 = new HtmlTableCell();
                //DropDownList ddlLeasedType = new DropDownList();
                //ddlLeasedType.ID = LstLeasedType[i].ToString();
                //ddlLeasedType.Width = Unit.Percentage(99);
                //ddlLeasedType.CssClass = "input";
                //ddlLeasedType.AutoPostBack = true;
                //ddlLeasedType.ForeColor = System.Drawing.Color.Black;
                //ddlLeasedType.SelectedIndexChanged += new EventHandler(ddlLeasedType_SelectedIndexChanged);
                //htcell17.Controls.Add(ddlLeasedType);

                htRow.Cells.Add(htcell1);
                htRow.Cells.Add(htcell2);
                //htRow.Cells.Add(htcell3);
                //htRow.Cells.Add(htcell4);
                htRow.Cells.Add(htcell5);
                htRow.Cells.Add(htcell6);
                htRow.Cells.Add(htcell7);
                //htRow.Cells.Add(htcell17);
                htRow.Cells.Add(htcell8);
                htRow.Cells.Add(htcell9);
                htRow.Cells.Add(htcell16);
                htRow.Cells.Add(htcell13);
                htRow.Cells.Add(htcell10);
                htRow.Cells.Add(htcell11);
                htRow.Cells.Add(htcell12);
                htRow.Cells.Add(htcell14);
                htRow.Cells.Add(htcell15);
                tblissueqty.Rows.Add(htRow);
            }
        }
    }
    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> GetSlipNo(string prefixText)
    {        
        Datautility db = new Datautility();
        DataTable dt = db.getdata("select distinct ORS.slipno from IVR_OnlineRegistrationSlip ORS inner join IVR_OnlineRegistration_Detail ORD on ors.RegId=ord.RegId where ors.slipno like '%" + prefixText + "%' and CAST(ISNULL(ord.Requestqty,0) as decimal(13,2)) != CAST(ISNULL(ord.IssueQty,0) as decimal(13,2));");
        List<string> SlipNo = new List<string>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            SlipNo.Add(Convert.ToString(dt.Rows[i]["slipno"]));
        }
        return SlipNo;
    }
    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> IssuedTo(string prefixText)
    {
        Datautility db = new Datautility();
        DataTable dt = db.getdata("select ISNULL(Firstname,'')+' '+ ISNULL(lastname,'') as Name from login where GROUPID=0 and Firstname  like '%" + prefixText + "%'");
        List<string> IssuedTo = new List<string>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            IssuedTo.Add(Convert.ToString(dt.Rows[i]["Name"]));
        }
        return IssuedTo;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        else
        {
            string Pagepermission = "select PAGE_ID,PAGE_ADD,PAGE_VIEW,USERID,USERNAME as EmailID from USER_PERMISSION inner join LOGIN on USER_PERMISSION.USER_ID=LOGIN.USERID where USER_ID='" + Session["userid"].ToString() + "' and PAGE_ID=1031";
            DataTable dt = DU.getdata(Pagepermission);
            if (dt.Rows.Count > 0)
            {
                if (Request.QueryString["mode"] != null)
                {
                    if (Request.QueryString["mode"].ToString() == "Save")
                    {
                        if (dt.Rows[0]["PAGE_ADD"].ToString() == "True")
                        {
                            if (!IsPostBack)
                            {
                                lblmsg.Visible = true;
                                pnlview.Visible = false;                                
                                pnlslipdetail.Visible = true;                                
                                if (Request.QueryString["SlipNo"] != null)
                                {
                                    lblmsg.Text = "Item Issued SuccessFully. Slip No:-" + Request.QueryString["SlipNo"].ToString();
                                    lblmsg.ForeColor = System.Drawing.Color.Green;
                                }
                                txtslipno.Focus();
                            }
                        }
                        else
                        {
                            Response.Redirect("~/AccessDenied.aspx");
                        }
                    }
                    if (Request.QueryString["mode"].ToString() == "View")
                    {
                        if (dt.Rows[0]["PAGE_VIEW"].ToString() == "True")
                        {
                            pnldata.Visible = false;
                            pnlslipdetail.Visible = false;                            
                            pnlview.Visible = true;
                            GrdViewIssuedItem();
                        }
                        else
                        {
                            Response.Redirect("~/AccessDenied.aspx");
                        }
                    }
                }
            }
            else
            {
                Response.Redirect("~/AccessDenied.aspx");
            }
        }
    }
    private void GrdViewIssuedItem()
    {
        dtDetail = DU.getdatausingprocedure("USP_IVR_IssueItemViewDetail");
        if (dtDetail.Rows.Count > 0)
        {
            grdview.DataSource = dtDetail;
            grdview.DataBind();
        }
    }
    private void _DeleteRows()
    {
        LstItemCode = (List<string>)ViewState["LstItemCode"];
        LstItemDesc = (List<string>)ViewState["LstItemDesc"];
        //LstPeCode = (List<string>)ViewState["LstPeCode"];
        //LstProductDesc = (List<string>)ViewState["LstProductDesc"];
        LstRO = (List<string>)ViewState["LstRO"];
        LstCustomer = (List<string>)ViewState["LstCustomer"];
        LstProjectName = (List<string>)ViewState["LstProjectName"];
        //LstLeasedType = (List<string>)ViewState["LstLeasedType"];
        LstRequestQty = (List<string>)ViewState["LstRequestQty"];
        LstAvailQty = (List<string>)ViewState["LstAvailQty"];
        LstIssueQty = (List<string>)ViewState["LstIssueQty"];
        LstBalanceQTY = (List<string>)ViewState["LstBalanceQTY"];
        LstItemid = (List<string>)ViewState["LstItemid"];
        LstTitid = (List<string>)ViewState["LstTitid"];
        LstRegid = (List<string>)ViewState["LstRegid"];
        LstItemQty = (List<string>)ViewState["LstItemQty"];
        LstIssuedQty = (List<string>)ViewState["LstIssuedQty"];

        for (int i = tblissueqty.Rows.Count; i > 1; i--)
        {
            if (Session["hidCounter"] != null)
            {
                LstItemCode.RemoveAt(i - 2);
                LstItemDesc.RemoveAt(i - 2);
                //LstPeCode.RemoveAt(i - 2);
                //LstProductDesc.RemoveAt(i - 2);
                LstRO.RemoveAt(i - 2);
                LstCustomer.RemoveAt(i - 2);
                LstProjectName.RemoveAt(i - 2);
                //LstLeasedType.RemoveAt(i - 2);
                LstAvailQty.RemoveAt(i - 2);
                LstRequestQty.RemoveAt(i - 2);
                LstIssueQty.RemoveAt(i - 2);
                LstBalanceQTY.RemoveAt(i - 2);
                LstItemid.RemoveAt(i - 2);
                LstTitid.RemoveAt(i - 2);
                LstRegid.RemoveAt(i - 2);
                LstItemQty.RemoveAt(i - 2);
                LstIssuedQty.RemoveAt(i - 2);
            }
            tblissueqty.Rows.RemoveAt(i - 1);
        }
        tblCounter = 0;
    }
    protected void AddRow()
    {
        tblCounter++;
        HtmlTableRow htRow = new HtmlTableRow();
        //ItemCode
        HtmlTableCell htcell1 = new HtmlTableCell();
        TextBox txtItemCode = new TextBox();
        txtItemCode.ID = "txtItemCode" + tblCounter.ToString();
        txtItemCode.Width = Unit.Percentage(99);
        txtItemCode.CssClass = "labelText";
        txtItemCode.ForeColor = System.Drawing.Color.Black;
        txtItemCode.Enabled = false;
        htcell1.Controls.Add(txtItemCode);
        LstItemCode.Add(txtItemCode.ID);
        HiddenField Hid = new HiddenField();
        Hid.ID = "Hid" + tblCounter.ToString();
        htcell1.Controls.Add(Hid);
        LstID.Add(Hid.ID);

        //ItemDescription
        HtmlTableCell htcell2 = new HtmlTableCell();
        TextBox txtItemDesc = new TextBox();
        txtItemDesc.ID = "txtItemDesc" + tblCounter.ToString();
        txtItemDesc.Width = Unit.Percentage(99);
        txtItemDesc.CssClass = "labelText";
        txtItemDesc.ForeColor = System.Drawing.Color.Black;
        txtItemDesc.Enabled = false;
        htcell2.Controls.Add(txtItemDesc);
        LstItemDesc.Add(txtItemDesc.ID);

        //HtmlTableCell htcell3 = new HtmlTableCell();
        //TextBox txtpecode = new TextBox();
        //txtpecode.ID = "txtpecode" + tblCounter.ToString();
        //txtpecode.Width = Unit.Percentage(99);
        //txtpecode.Enabled = false;
        //txtpecode.CssClass = "labelText";
        //txtpecode.ForeColor = System.Drawing.Color.Black;
        //htcell3.Controls.Add(txtpecode);
        //LstPeCode.Add(txtpecode.ID);

        //HtmlTableCell htcell4 = new HtmlTableCell();
        //TextBox txtProductDesc = new TextBox();
        //txtProductDesc.ID = "txtProductDesc" + tblCounter.ToString();
        //txtProductDesc.Width = Unit.Percentage(99);
        //txtProductDesc.Enabled = false;
        //txtProductDesc.ForeColor = System.Drawing.Color.Black;
        //txtProductDesc.CssClass = "labelText";
        //htcell4.Controls.Add(txtProductDesc);
        //LstProductDesc.Add(txtProductDesc.ID);


        //RO#
        HtmlTableCell htcell5 = new HtmlTableCell();
        TextBox txtRO = new TextBox();
        txtRO.ID = "txtRO" + tblCounter.ToString();
        txtRO.Width = Unit.Percentage(99);
        txtRO.Enabled = false;
        txtRO.ForeColor = System.Drawing.Color.Black;
        txtRO.CssClass = "labelText";
        htcell5.Controls.Add(txtRO);
        LstRO.Add(txtRO.ID);

        //Customer
        HtmlTableCell htcell6 = new HtmlTableCell();
        TextBox txtCustomer = new TextBox();
        txtCustomer.ID = "txtCustomer" + tblCounter.ToString();
        txtCustomer.Width = Unit.Percentage(99);
        txtCustomer.ForeColor = System.Drawing.Color.Black;
        txtCustomer.Enabled = false;
        txtCustomer.CssClass = "labelText";
        htcell6.Controls.Add(txtCustomer);
        LstCustomer.Add(txtCustomer.ID);
        HiddenField hidCustomer = new HiddenField();
        hidCustomer.ID = "hidCustomer" + tblCounter.ToString();
        htcell6.Controls.Add(hidCustomer);
        LstCustomerID.Add(hidCustomer.ID);

        //Project
        HtmlTableCell htcell7 = new HtmlTableCell();
        TextBox txtProjectname = new TextBox();
        txtProjectname.ID = "txtProjectname" + tblCounter.ToString();
        txtProjectname.Width = Unit.Percentage(99);
        txtProjectname.ForeColor = System.Drawing.Color.Black;
        txtProjectname.CssClass = "labelText";
        txtProjectname.Enabled = false;
        htcell7.Controls.Add(txtProjectname);
        LstProjectName.Add(txtProjectname.ID);
        HiddenField hidProject = new HiddenField();
        hidProject.ID = "hidProject" + tblCounter.ToString();
        htcell7.Controls.Add(hidProject);
        LstProjectID.Add(hidProject.ID);

        //Available Qty
        HtmlTableCell htcell8 = new HtmlTableCell();
        TextBox txtAvailableqty = new TextBox();
        txtAvailableqty.ID = "txtAvailableqty" + tblCounter.ToString();
        txtAvailableqty.Width = Unit.Percentage(99);
        txtAvailableqty.Enabled = false;
        txtAvailableqty.ForeColor = System.Drawing.Color.Black;
        txtAvailableqty.CssClass = "inputnumber";
        htcell8.Controls.Add(txtAvailableqty);
        LstAvailQty.Add(txtAvailableqty.ID);


        //Requested Qty
        HtmlTableCell htcell9 = new HtmlTableCell();
        TextBox lblrequestqty = new TextBox();
        lblrequestqty.ID = "lblrequestqty" + tblCounter.ToString();
        lblrequestqty.Width = Unit.Percentage(99);
        lblrequestqty.Enabled = false;
        lblrequestqty.ForeColor = System.Drawing.Color.Black;
        lblrequestqty.CssClass = "inputnumber";
        htcell9.Controls.Add(lblrequestqty);
        LstRequestQty.Add(lblrequestqty.ID);


        //Issue Qty
        HtmlTableCell htcell10 = new HtmlTableCell();
        TextBox txtIssueQty = new TextBox();
        txtIssueQty.ID = "txtIssueQty" + tblCounter.ToString();
        txtIssueQty.Width = Unit.Percentage(99);
        txtIssueQty.CssClass = "inputnumber";
        //txtIssueQty.Attributes.Add("onblur", "return BalanceQty(" + tblCounter.ToString() + ");");        
        txtIssueQty.Attributes.Add("onfocus", "if(this.value=='0') this.value='';");
        txtIssueQty.Attributes.Add("onblur", "return CheckValue(this.id);");
        txtIssueQty.Attributes.Add("onKeyPress", "return isNumber(event);");
        htcell10.Controls.Add(txtIssueQty);
        LstIssueQty.Add(txtIssueQty.ID);

        HtmlTableCell htcell11 = new HtmlTableCell();
        HiddenField hidItemid = new HiddenField();
        hidItemid.ID = "hidItemid" + tblCounter.ToString();
        htcell11.Controls.Add(hidItemid);
        LstItemid.Add(hidItemid.ID);

        HtmlTableCell htcell12 = new HtmlTableCell();
        HiddenField hidTITID = new HiddenField();
        hidTITID.ID = "hidTITID" + tblCounter.ToString();
        htcell12.Controls.Add(hidTITID);
        LstTitid.Add(hidTITID.ID);


        //Balance QTY
        HtmlTableCell htcell13 = new HtmlTableCell();
        TextBox txtBalanceQty = new TextBox();
        txtBalanceQty.ID = "txtBalanceQty" + tblCounter.ToString();
        txtBalanceQty.Width = Unit.Percentage(99);
        txtBalanceQty.CssClass = "inputnumber";
        txtBalanceQty.Enabled = false;
        txtBalanceQty.ForeColor = System.Drawing.Color.Black;
        htcell13.Controls.Add(txtBalanceQty);
        LstBalanceQTY.Add(txtBalanceQty.ID);

        HtmlTableCell htcell14 = new HtmlTableCell();
        HiddenField hidregid = new HiddenField();
        hidregid.ID = "hidregid" + tblCounter.ToString();
        htcell14.Controls.Add(hidregid);
        LstRegid.Add(hidregid.ID);

        HtmlTableCell htcell15 = new HtmlTableCell();
        HiddenField hiditemqty = new HiddenField();
        hiditemqty.ID = "hiditemqty" + tblCounter.ToString();
        htcell15.Controls.Add(hiditemqty);
        LstItemQty.Add(hiditemqty.ID);


        //Issued Qty
        HtmlTableCell htcell16 = new HtmlTableCell();
        TextBox txtIssuedQty = new TextBox();
        txtIssuedQty.ID = "txtIssuedQty" + tblCounter.ToString();
        txtIssuedQty.Width = Unit.Percentage(99);
        txtIssuedQty.CssClass = "inputnumber";
        txtIssuedQty.Enabled = false;
        txtIssuedQty.ForeColor = System.Drawing.Color.Black;
        htcell16.Controls.Add(txtIssuedQty);
        LstIssuedQty.Add(txtIssuedQty.ID);


        //LeasedType
        //HtmlTableCell htcell17 = new HtmlTableCell();
        //DropDownList ddlLeasedType = new DropDownList();
        //ddlLeasedType.ID = "ddlLeasedType" + tblCounter.ToString();
        //ddlLeasedType.Width = Unit.Percentage(99);
        //ddlLeasedType.CssClass = "input";
        //ddlLeasedType.AutoPostBack = true;
        //ddlLeasedType.ForeColor = System.Drawing.Color.Black;
        //ddlLeasedType.SelectedIndexChanged +=new EventHandler(ddlLeasedType_SelectedIndexChanged);
        //htcell17.Controls.Add(ddlLeasedType);
        //LstLeasedType.Add(ddlLeasedType.ID);

        htRow.Cells.Add(htcell1);
        htRow.Cells.Add(htcell2);
        //htRow.Cells.Add(htcell3);
        //htRow.Cells.Add(htcell4);
        htRow.Cells.Add(htcell5);
        htRow.Cells.Add(htcell6);
        htRow.Cells.Add(htcell7);
        //htRow.Cells.Add(htcell17);
        htRow.Cells.Add(htcell8);
        htRow.Cells.Add(htcell9);
        htRow.Cells.Add(htcell16);
        htRow.Cells.Add(htcell13);
        htRow.Cells.Add(htcell10);
        htRow.Cells.Add(htcell11);
        htRow.Cells.Add(htcell12);
        htRow.Cells.Add(htcell14);
        htRow.Cells.Add(htcell15);
        tblissueqty.Rows.Add(htRow);

        ViewState["LstID"] = LstID;
        ViewState["LstItemCode"] = LstItemCode;
        ViewState["LstRegid"] = LstRegid;
        ViewState["LstItemDesc"] = LstItemDesc;
        //ViewState["LstPeCode"] = LstPeCode;
        //ViewState["LstProductDesc"] = LstProductDesc;
        ViewState["LstRO"] = LstRO;
        ViewState["LstCustomer"] = LstCustomer;
        ViewState["LstProjectName"] = LstProjectName;
        ViewState["LstCustomerID"] = LstCustomerID;
        ViewState["LstProjectID"] = LstProjectID;
        //ViewState["LstLeasedType"] = LstLeasedType;
        ViewState["LstRequestQty"] = LstRequestQty;
        ViewState["LstAvailQty"] = LstAvailQty;
        ViewState["LstIssueQty"] = LstIssueQty;
        ViewState["LstItemid"] = LstItemid;
        ViewState["LstTitid"] = LstTitid;
        ViewState["LstBalanceQTY"] = LstBalanceQTY;
        ViewState["LstItemQty"] = LstItemQty;
        ViewState["LstIssuedQty"] = LstIssuedQty;
        Session["hidCounter"] = tblCounter.ToString();
    }
    protected void ddlLeasedType_SelectedIndexChanged(object sender, EventArgs e)
    {
        double TotalQty = 0.00;
        DropDownList DDLLEASED = (DropDownList)sender;
        string LeasedValue = DDLLEASED.SelectedValue;
        int rowindex = Convert.ToInt32(DDLLEASED.ID.ToString().Substring(13));
        TextBox txtAvailableqty = (TextBox)tblissueqty.Rows[rowindex].FindControl("txtAvailableqty" + rowindex.ToString());
        HiddenField hiditemid = (HiddenField)tblissueqty.Rows[rowindex].FindControl("hiditemid" + rowindex.ToString());
        string SqlLeased = "select ISNULL(SUM(BalanceQty),0) as BalanceQty from IVR_TRAN_ITEM TI inner join IVR_TRAN_MAIN TM on TI.TRNID=TM.TRNID where TI.ITMID=" + hiditemid.Value + " and TM.Leasedtype='" + LeasedValue + "' Union All select ISNULL(SUM(BalanceQty),0) from IVR_ITEMOPENBAL where ITMID=" + hiditemid.Value + " and Leasedtype='" + LeasedValue + "'";
        DataTable DTLeasedType = DU.getdata(SqlLeased);
        if (DTLeasedType.Rows.Count > 0)
        {
            for (int i = 0; i < DTLeasedType.Rows.Count; i++)
            {
                TotalQty += Convert.ToDouble(DTLeasedType.Rows[i]["BalanceQty"].ToString());
            }
        }
        txtAvailableqty.Text = TotalQty.ToString();
        //lblmsg.Text = hiditemid.Value;
        //lblmsg.Visible = true;
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        GetSlipDetail();
        pnldata.Visible = true;
        tblCaptcha.Visible = true;        
    }
    protected void grdview_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdview.PageIndex = e.NewPageIndex;
        GrdViewIssuedItem();
    }
    private void ClearTable()
    {
        if (tblissueqty.Rows.Count > 1)
        {
            for (int i = tblissueqty.Rows.Count - 1; i > 0; i--)
            {
                tblissueqty.Rows.RemoveAt(i);
            }
            hidCounter.Value = "0";
            tblCounter = 0;
            ViewState["LstItemCode"] = null;
            ViewState["LstItemDesc"] = null;
            ViewState["LstRequestQty"] = null;
            ViewState["LstIssueQty"] = null;
            ViewState["LstHiddenid"] = null;
            Session["hidCounter"] = null;



            List<string> LstItemCode = new List<string>();
            List<string> LstItemDesc = new List<string>();
            List<string> LstRequestQty = new List<string>();
            List<string> LstIssueQty = new List<string>();
            List<string> LstHiddenid = new List<string>();
            pnldata.Visible = false;
        }
    }
    private void GetSlipDetail()
    {
        try
        {
            string sqlGetSlip = "select regid,reqdate,username from IVR_OnlineRegistrationSlip where slipno=" + txtslipno.Text;
            DataTable DtSlipNo = DU.getdata(sqlGetSlip);
            if (DtSlipNo.Rows.Count > 0)
            {
                hidCounter.Value = DtSlipNo.Rows.Count.ToString();
                txtrequestedDate.Text = Convert.ToDateTime(DtSlipNo.Rows[0]["reqdate"].ToString()).ToShortDateString();
                txtRequestedBy.Text = DtSlipNo.Rows[0]["username"].ToString().ToUpper();
                BindUsername();
            }
            string sqlGetSlipDetails = "select id,Regid,ItemCode,ItemDesc,ISNULL(IssueQty,0) as ISSUEQTY,RO,ACC_LEDMAST.LNAME as CustomerName,Customer,Project,MAS_PROJECT.projectname as ProjectName,Item_ItemID,ISNULL(RequestQty,0) as RequestQty from IVR_OnlineRegistration_Detail inner join ACC_LEDMAST on ACC_LEDMAST.LID=IVR_OnlineRegistration_Detail.Customer inner join MAS_PROJECT on MAS_PROJECT.projectid=IVR_OnlineRegistration_Detail.Project  where RegId=" + DtSlipNo.Rows[0]["regid"].ToString();
            DataTable DtSlipDetails = DU.getdata(sqlGetSlipDetails);
            if (DtSlipDetails.Rows.Count > 0)
            {
                for (int j = 0; j < DtSlipDetails.Rows.Count; j++)
                {
                    AddRow();
                }
                for (int i = 1; i <= DtSlipDetails.Rows.Count; i++)
                {
                    HiddenField Hid = (HiddenField)tblissueqty.Rows[i].FindControl("Hid" + i.ToString());
                    TextBox txtItemCode = (TextBox)tblissueqty.Rows[i].FindControl("txtItemCode" + i.ToString());
                    TextBox txtItemDesc = (TextBox)tblissueqty.Rows[i].FindControl("txtItemDesc" + i.ToString());
                    TextBox txtIssueQty = (TextBox)tblissueqty.Rows[i].FindControl("txtIssueQty" + i.ToString());
                    TextBox txtRO = (TextBox)tblissueqty.Rows[i].FindControl("txtRO" + i.ToString());
                    TextBox txtCustomer = (TextBox)tblissueqty.Rows[i].FindControl("txtCustomer" + i.ToString());
                    TextBox txtprojectName = (TextBox)tblissueqty.Rows[i].FindControl("txtProjectname" + i.ToString());
                    TextBox lblRequestQty = (TextBox)tblissueqty.Rows[i].FindControl("lblrequestqty" + i.ToString());
                    HiddenField hiditemid = (HiddenField)tblissueqty.Rows[i].FindControl("hidItemid" + i.ToString());
                    TextBox txtAvailable = (TextBox)tblissueqty.Rows[i].FindControl("txtAvailableqty" + i.ToString());
                    TextBox txtIssuedQty = (TextBox)tblissueqty.Rows[i].FindControl("txtIssuedQty" + i.ToString());
                    TextBox txtbalanceQty = (TextBox)tblissueqty.Rows[i].FindControl("txtBalanceQty" + i.ToString());
                    HiddenField hidtitid = (HiddenField)tblissueqty.Rows[i].FindControl("hidTITID" + i.ToString());
                    HiddenField hidregid = (HiddenField)tblissueqty.Rows[i].FindControl("hidregid" + i.ToString());
                    HiddenField hidCustomer = (HiddenField)tblissueqty.Rows[i].FindControl("hidCustomer" + i.ToString());
                    HiddenField hidProject = (HiddenField)tblissueqty.Rows[i].FindControl("hidProject" + i.ToString());

                    Hid.Value = DtSlipDetails.Rows[i - 1]["Id"].ToString();
                    txtItemCode.Text = DtSlipDetails.Rows[i - 1]["ItemCode"].ToString();
                    txtItemDesc.Text = DtSlipDetails.Rows[i - 1]["ItemDesc"].ToString();
                    txtIssueQty.Text = "0";
                    txtRO.Text = DtSlipDetails.Rows[i - 1]["RO"].ToString();
                    txtCustomer.Text = DtSlipDetails.Rows[i - 1]["CustomerName"].ToString();
                    txtprojectName.Text = DtSlipDetails.Rows[i - 1]["ProjectName"].ToString();
                    lblRequestQty.Text = Convert.ToInt32(Convert.ToDouble(DtSlipDetails.Rows[i - 1]["RequestQTY"].ToString())).ToString();
                    txtIssuedQty.Text = Convert.ToInt32(Convert.ToDouble(DtSlipDetails.Rows[i - 1]["IssueQty"].ToString())).ToString();
                    hidCustomer.Value = DtSlipDetails.Rows[i - 1]["Customer"].ToString();
                    hidProject.Value = DtSlipDetails.Rows[i - 1]["Project"].ToString();
                    hidregid.Value = DtSlipDetails.Rows[i - 1]["Regid"].ToString();

                    string TITID = "";
                    string SqlItemId = "select ITMID from IVR_ITEM where ITMCODE='" + txtItemCode.Text + "'";
                    DataTable DTITEM = DU.getdata(SqlItemId);
                    if (DTITEM.Rows.Count > 0)
                    {
                        string sqlItem = "select TITID,ITMQTY,coalesce(issueqty,0)as ISSUEQTY,BalanceQTY from IVR_TRAN_ITEM inner join IVR_TRAN_MAIN on IVR_TRAN_MAIN.TRNID=ivr_TRAN_ITEM.TRNID  where ITMID='" + DTITEM.Rows[0]["ITMID"] + "'";
                        string sqlOpening = "select SLNUM,ITMID,QUANTITY,ISNULL(BalanceQTY,0) as BalanceQTY from IVR_ITEMOPENBAL where ITMID='" + DTITEM.Rows[0]["ITMID"] + "'";
                        DataTable dtTranItem = DU.getdata(sqlItem);
                        DataTable dtopeningstock = DU.getdata(sqlOpening);
                        double qty = 0;
                        if (dtTranItem.Rows.Count > 0 || dtopeningstock.Rows.Count > 0)
                        {
                            for (int j = 0; j < dtopeningstock.Rows.Count; j++)
                            {
                                qty = qty + Convert.ToDouble(dtopeningstock.Rows[j]["BalanceQTY"].ToString());
                                TITID += dtopeningstock.Rows[j]["SLNUM"].ToString() + "_";
                            }
                            for (int k = 0; k < dtTranItem.Rows.Count; k++)
                            {
                                qty = qty + Convert.ToDouble(dtTranItem.Rows[k]["BalanceQTY"].ToString());
                                TITID += "," + dtTranItem.Rows[k]["TITID"].ToString();
                            }
                            txtAvailable.Text = qty.ToString();
                            hiditemid.Value = DTITEM.Rows[0]["ITMID"].ToString();
                        }
                        //txtIssuedQty.Text = Convert.ToDouble(TotalissuedQty.ToString()).ToString("N0");
                        if (TITID.Length > 1)
                        {
                            hidtitid.Value = TITID.ToString();
                        }
                        txtbalanceQty.Text = (Convert.ToInt32(lblRequestQty.Text) - Convert.ToInt32(txtIssuedQty.Text)).ToString();
                        if (txtbalanceQty.Text.Contains('-'))
                        {
                            txtbalanceQty.Text = "0";
                        }
                    }
                }
            }
        }
        catch (Exception Ex)
        {

        }
        #region
        //try
        //{
        //    lblmsg.Text = ""; ClearTable();
        //    SqlParameter[] param = { new SqlParameter("@slipno", txtslipno.Text) };
        //    DataTable Dt = DU.getdatausingprocedure("USP_IVR_IssueItemDetail", param);  //USP_IVR_IssueItemDetail
        //    int CountRow = Dt.Rows.Count;
        //    if (Dt.Rows.Count > 0)
        //    {
        //        hidCounter.Value = Dt.Rows.Count.ToString();
        //        txtrequestedDate.Text = Convert.ToDateTime(Dt.Rows[0]["reqdate"].ToString()).ToShortDateString();
        //        //txtleasedtype.Text = Dt.Rows[0]["LeasedType"].ToString();
        //        txtRequestedBy.Text = Dt.Rows[0]["RequestedBy"].ToString().ToUpper();

        //        //ListItem LstLeasedTypeowned = new ListItem();
        //        //LstLeasedTypeowned.Text = "Owned";
        //        //LstLeasedTypeowned.Value = "Owned";

        //        //ListItem LstLeasedTypeConsigned = new ListItem();
        //        //LstLeasedTypeConsigned.Text = "Consigned";
        //        //LstLeasedTypeConsigned.Value = "Consigned";

        //        BindUsername();
        //        if (tblissueqty.Rows.Count > 1)
        //        {
        //            _DeleteRows();
        //        }
        //        for (int i = 0; i < Dt.Rows.Count; i++)
        //        {
        //            AddRow();
        //        }

        //        for (int i = 1; i <= CountRow; i++)
        //        {
        //            TextBox txtItemCode = (TextBox)tblissueqty.Rows[i].FindControl("txtItemCode" + i.ToString());
        //            TextBox txtItemDesc = (TextBox)tblissueqty.Rows[i].FindControl("txtItemDesc" + i.ToString());
        //            TextBox txtIssueQty = (TextBox)tblissueqty.Rows[i].FindControl("txtIssueQty" + i.ToString());
        //            //TextBox txtProductDesc = (TextBox)tblissueqty.Rows[i].FindControl("txtProductDesc" + i.ToString());
        //            TextBox txtRO = (TextBox)tblissueqty.Rows[i].FindControl("txtRO" + i.ToString());
        //            TextBox txtCustomer = (TextBox)tblissueqty.Rows[i].FindControl("txtCustomer" + i.ToString());
        //            TextBox txtprojectName = (TextBox)tblissueqty.Rows[i].FindControl("txtProjectname" + i.ToString());
        //            //DropDownList DDLLeasedType = (DropDownList)tblissueqty.Rows[i].FindControl("ddlLeasedType" + i.ToString());
        //            TextBox lblRequestQty = (TextBox)tblissueqty.Rows[i].FindControl("lblrequestqty" + i.ToString());
        //            HiddenField hiditemid = (HiddenField)tblissueqty.Rows[i].FindControl("hidItemid" + i.ToString());
        //            TextBox txtAvailable = (TextBox)tblissueqty.Rows[i].FindControl("txtAvailableqty" + i.ToString());
        //            TextBox txtIssuedQty = (TextBox)tblissueqty.Rows[i].FindControl("txtIssuedQty" + i.ToString());
        //            TextBox txtbalanceQty = (TextBox)tblissueqty.Rows[i].FindControl("txtBalanceQty" + i.ToString());
        //            HiddenField hidtitid = (HiddenField)tblissueqty.Rows[i].FindControl("hidTITID" + i.ToString());
        //            HiddenField hidregid = (HiddenField)tblissueqty.Rows[i].FindControl("hidregid" + i.ToString());

        //            txtItemCode.Text = Dt.Rows[i - 1]["ItemCode"].ToString();
        //            txtItemDesc.Text = Dt.Rows[i - 1]["ItemDesc"].ToString();
        //            txtIssueQty.Text = "0";
        //            //txtProductDesc.Text = Dt.Rows[i - 1]["ProductDesc"].ToString();
        //            txtRO.Text = Dt.Rows[i - 1]["RO"].ToString();
        //            txtCustomer.Text = Dt.Rows[i - 1]["CustomerName"].ToString();
        //            txtprojectName.Text = Dt.Rows[i - 1]["ProjectName"].ToString();
        //            lblRequestQty.Text = Convert.ToDouble(Dt.Rows[i - 1]["RequestQTY"].ToString()).ToString("N0");
        //            hiditemid.Value = Dt.Rows[i - 1]["Item_ItemID"].ToString();
        //            hidregid.Value = Dt.Rows[i - 1]["Regid"].ToString();

        //            //DDLLeasedType.Items.Add(LstLeasedTypeowned);
        //            //DDLLeasedType.Items.Add(LstLeasedTypeConsigned);
        //            //DDLLeasedType.Items.Insert(0, "-- Leased Type--");

        //            //string sqlavailableqty = "select TITID,ITMQTY,isnull(ISSUEQTY,0) as ISSUEQTY,BalanceQty from IVR_TRAN_ITEM inner join IVR_TRAN_MAIN on IVR_TRAN_ITEM.TRNID=IVR_TRAN_MAIN.TRNID  where IVR_TRAN_ITEM.ITMID='" + hiditemid.Value + "' and IVR_TRAN_MAIN.leasedtype='" + txtleasedtype.Text + "'";
        //            //DataTable DtAvailable = DU.getdata(sqlavailableqty);
        //            //string sqlopeningstock = "select SLNUM, ITMID, QUANTITY, BalanceQty, IssueQty from IVR_ITEMOPENBAL where ITMID='" + hiditemid.Value + "' and LeasedType='" + txtleasedtype.Text + "'";
        //            //DataTable DtOpStock = DU.getdata(sqlopeningstock);

        //            string SqlRequest = "select ITEM_TITID,ITEM_ItemQty,OPSLNUM,ITEM_ItemID,OPQuantity,ISNULL(IssueQty,0) as IssueQty from IVR_OnlineRegistration_Detail ORD inner join IVR_OnlineRegistrationSlip ORS on ORD.RegId=ORS.RegId  where ITEM_ItemID='" + hiditemid.Value + "' and ORS.slipno='" + txtslipno.Text + "'";
        //            DataTable DTRequest = DU.getdata(SqlRequest);

        //            double TotalAvailableQty = 0; double TotalissuedQty = 0; string TITID = "";
        //            if (DTRequest.Rows.Count > 0)
        //            {
        //                for (int k = 0; k < DTRequest.Rows.Count; k++)
        //                {
        //                    TotalAvailableQty += Convert.ToDouble(DTRequest.Rows[k]["OPQuantity"].ToString());
        //                    if (DTRequest.Rows[k]["OPSLNUM"].ToString() != "" && DTRequest.Rows[k]["OPSLNUM"].ToString() != "0")
        //                    {
        //                        TotalissuedQty += Convert.ToDouble(DTRequest.Rows[k]["IssueQTY"].ToString());
        //                        TITID += DTRequest.Rows[k]["OPSLNUM"].ToString() + "_";
        //                    }
        //                }
        //                for (int j = 0; j < DTRequest.Rows.Count; j++)
        //                {
        //                    //TotalAvailableQty += Convert.ToDouble(DtAvailable.Rows[j]["ITMQTY"].ToString());BalanceQty
        //                    if (DTRequest.Rows[j]["ITEM_ItemQty"].ToString() != "")
        //                    {
        //                        TotalAvailableQty += Convert.ToDouble(DTRequest.Rows[j]["ITEM_ItemQty"].ToString());
        //                        TotalissuedQty += Convert.ToDouble(DTRequest.Rows[j]["ISSUEQTY"].ToString());
        //                    }
        //                    if (DTRequest.Rows[j]["ITEM_TITID"].ToString() != "" && DTRequest.Rows[j]["ITEM_TITID"].ToString() != "0")
        //                    {
        //                        TITID += "," + DTRequest.Rows[j]["ITEM_TITID"].ToString();
        //                    }
        //                }
        //            }
        //            txtAvailable.Text = Convert.ToDouble(TotalAvailableQty.ToString()).ToString("N0");
        //            txtIssuedQty.Text = Convert.ToDouble(TotalissuedQty.ToString()).ToString("N0");
        //            if (TITID.Length > 1)
        //            {
        //                hidtitid.Value = TITID.ToString();
        //            }
        //            txtbalanceQty.Text = (Convert.ToDouble(lblRequestQty.Text) - Convert.ToDouble(txtIssuedQty.Text)).ToString("N0");
        //            if (txtbalanceQty.Text.Contains('-'))
        //            {
        //                txtbalanceQty.Text = "0";
        //            }
        //        }
        //    }
        //}
        //catch (Exception ex)
        //{
        //}
        #endregion
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string strItemIssueResult = "";        
        try
        {
            //bool IsValidate = ucCaptcha.Validate(txtcaptcha.Text.Trim());
            //if (IsValidate)
            if (SumCaptchaControl1.Success)
            {
                //Intert Data With the Transaction.
                //Insert value into the IVR_ISSUEITEM Table  
                if (txtslipno.Text != "")
                {
                    strItemIssueResult += " Exec USP_IVR_ITEM_ISSUE " + txtslipno.Text + ",'" + Convert.ToDateTime(txtrequestedDate.Text).ToString(string.Format("yyyy/MM/dd")) + "','" + txtRequestedBy.Text + "','" + Convert.ToDateTime(txtIssuedDate.Text).ToString(string.Format("yyyy/MM/dd")) + "','" + txtissuedby.Text + "','" + txtissuedto.Text + "','" + Session["username"].ToString() + "';";
                    //Insert value into the IVR_ISSUEITEM_DETAILS Table
                    strItemIssueResult += " Declare @IssueID int set @IssueID=(SELECT IDENT_CURRENT('IVR_ISSUEITEM') AS IssueID); Declare @IssueDetailID int; Declare @IssueBatchID int;";
                    #region
                    for (int i = 1; i < tblissueqty.Rows.Count; i++)
                    {
                        int flag = 0;
                        #region find the Control
                        HiddenField Hid = (HiddenField)tblissueqty.Rows[i].FindControl("Hid" + i.ToString());
                        TextBox txtIssueQty = (TextBox)tblissueqty.Rows[i].FindControl("txtIssueQty" + i.ToString());
                        TextBox txtItemCode = (TextBox)tblissueqty.Rows[i].FindControl("txtItemCode" + i.ToString());
                        TextBox txtItemDesc = (TextBox)tblissueqty.Rows[i].FindControl("txtItemDesc" + i.ToString());
                        TextBox txtRO = (TextBox)tblissueqty.Rows[i].FindControl("txtRO" + i.ToString());
                        TextBox txtCustomer = (TextBox)tblissueqty.Rows[i].FindControl("txtCustomer" + i.ToString());
                        TextBox txtprojectName = (TextBox)tblissueqty.Rows[i].FindControl("txtProjectname" + i.ToString());
                        TextBox lblRequestQty = (TextBox)tblissueqty.Rows[i].FindControl("lblrequestqty" + i.ToString());
                        HiddenField hidregid = (HiddenField)tblissueqty.Rows[i].FindControl("hidregid" + i.ToString());
                        HiddenField hidItemid = (HiddenField)tblissueqty.Rows[i].FindControl("hidItemid" + i.ToString());
                        HiddenField hidTitid = (HiddenField)tblissueqty.Rows[i].FindControl("hidTITID" + i.ToString());
                        HiddenField hidCustomer = (HiddenField)tblissueqty.Rows[i].FindControl("hidCustomer" + i.ToString());
                        HiddenField hidProject = (HiddenField)tblissueqty.Rows[i].FindControl("hidProject" + i.ToString());
                        //string GetITMID = "select ITMID from IVR_ITEM where ITMCODE='" + txtItemCode.Text + "'";
                        #endregion
                        strItemIssueResult += " EXEC USP_IVR_ISSUEITEM_DETAILS  @IssueID ," + hidregid.Value + "," + hidItemid.Value + ",'" + txtItemCode.Text + "','" + txtItemDesc.Text + "','" + txtRO.Text + "'," + hidCustomer.Value + "," + hidProject.Value + "," + Convert.ToInt32(lblRequestQty.Text) + "," + Convert.ToInt32(txtIssueQty.Text) + ",'" + Session["username"].ToString() + "';";
                        strItemIssueResult += "set @IssueDetailID=(SELECT IDENT_CURRENT('IVR_ISSUEITEM_DETAILS') AS IssueDetailID);";
                        //For the IssueitemDetail
                        #region
                        //if (DtIssueITemDetails.Rows.Count > 0)
                        //{
                        //Insert value into the IVR_ISSUEITEM_BATCHDETAILS Table
                        double temprate = 0; double Openingvalue = 0; double IssueQty = 0; double BalanceQty = 0; double Checkissueqty = 0;
                        string OPBALSQL = "select ISNULL(QUANTITY,0) as QUANTITY,ISNULL(AMOUNT,0) as AMOUNT,BALANCEQTY,SLNUM,OPDATE from IVR_ITEMOPENBAL where ITMID=" + hidItemid.Value;
                        DataTable DtOPBAL = DU.getdata(OPBALSQL);
                        if (DtOPBAL.Rows.Count > 0)
                        {
                            //if Issue qty is less than BanceQty 
                            #region
                            IssueQty = Convert.ToDouble(txtIssueQty.Text);
                            BalanceQty = Convert.ToDouble(DtOPBAL.Rows[0]["BALANCEQTY"].ToString());
                            #region ISSUE QTY is less than opening balance qty
                            if (IssueQty > 0)
                            {
                                if (IssueQty <= BalanceQty)
                                {
                                    if (Convert.ToDouble(DtOPBAL.Rows[0]["AMOUNT"].ToString()) > 0)
                                    {
                                        temprate = Convert.ToDouble(DtOPBAL.Rows[0]["AMOUNT"].ToString()) / Convert.ToDouble(DtOPBAL.Rows[0]["QUANTITY"].ToString());
                                    }
                                    Openingvalue = temprate * Convert.ToInt32(txtIssueQty.Text);

                                    strItemIssueResult += " Exec USP_IVR_ISSUEITEM_BATCHDETAILS @IssueDetailID ," + Convert.ToInt32(DtOPBAL.Rows[0]["SLNUM"].ToString()) + "," + Convert.ToInt32(hidItemid.Value) + "," + Convert.ToInt32(txtIssueQty.Text) + "," + Convert.ToDecimal(temprate) + ",'" + Convert.ToDateTime(DtOPBAL.Rows[0]["OPDATE"].ToString()).ToString(string.Format("yyyy/MM/dd")) + "',null,null,null,null,null,null,null,null,null,null,null,null,null,null,null," + Convert.ToDecimal(Openingvalue) + ",'" + Session["username"].ToString() + "',null,'OP',null,null,null,null,null,null,null,null,null;";
                                    strItemIssueResult += " set @IssueBatchID=(SELECT IDENT_CURRENT('IVR_ISSUEITEM_BATCHDETAILS') AS IssueBatchID);";

                                    //insert value into the IVR_COMMONITEM_ISSUERETURN Table
                                    #region for insertion of the Common Table
                                    //DtIssueBatchItem = DU.getdata(getIssueBatchItem);
                                    //if (DtIssueBatchItem.Rows.Count > 0)
                                    //{                                    
                                    strItemIssueResult += " EXEC USP_IVR_COMMONITEM_ISSUERETURN " + Convert.ToInt32(DtOPBAL.Rows[0]["SLNUM"].ToString()) + ",@IssueID,@IssueDetailID,@IssueBatchID,null,null,null," + Convert.ToInt32(hidItemid.Value) + "," + Convert.ToInt32(txtIssueQty.Text) + "," + Convert.ToDecimal(temprate) + ",'" + Convert.ToDateTime(DtOPBAL.Rows[0]["OPDATE"].ToString()).ToString(string.Format("yyyy/MM/dd")) + "',null,null,null,null,null,null,null,null,null,null,null,null," + Convert.ToDecimal(Openingvalue) + ",'ISSUE','" + txtslipno.Text + "'," + Convert.ToInt32(hidregid.Value) + "," + Convert.ToInt32(txtIssueQty.Text) + "," + Convert.ToDecimal(Openingvalue) + ",'" + Convert.ToDateTime(txtIssuedDate.Text).ToString(string.Format("yyyy/MM/dd")) + "',null,null,null,null,null,null,null,null,null,null,null,null,null,null,null;";
                                    //}
                                    #endregion
                                    strItemIssueResult += " Update IVR_ITEMOPENBAL set ISSUEQTY=ISSUEQTY + " + txtIssueQty.Text + " where SLNUM=" + DtOPBAL.Rows[0]["SLNUM"].ToString() + ";";
                                    //DU.executesql(UpdateTable);
                                    Checkissueqty = Convert.ToDouble(txtIssueQty.Text);
                                }
                            }
                            #endregion
                            double TempPurchaseQty = 0; double TempOpeningQty = 0;
                            //Issue qty is Greater then Opening Balance Qty
                            #region Issue qty is Greater then Opening Balance Qty
                            if (IssueQty > 0)
                            {
                                if (IssueQty > BalanceQty)
                                {
                                    TempPurchaseQty = IssueQty - BalanceQty;
                                    if (BalanceQty > 0)
                                    {
                                        //Issue from the IVR_ITEMOPENBAL
                                        if (Convert.ToDouble(DtOPBAL.Rows[0]["AMOUNT"].ToString()) > 0)
                                        {
                                            temprate = Convert.ToDouble(DtOPBAL.Rows[0]["AMOUNT"].ToString()) / Convert.ToDouble(DtOPBAL.Rows[0]["QUANTITY"].ToString());
                                        }
                                        Openingvalue = temprate * BalanceQty;
                                        strItemIssueResult += " Exec USP_IVR_ISSUEITEM_BATCHDETAILS @IssueDetailID," + Convert.ToInt32(DtOPBAL.Rows[0]["SLNUM"].ToString()) + "," + Convert.ToInt32(hidItemid.Value) + "," + Convert.ToInt32(BalanceQty) + "," + Convert.ToDecimal(temprate) + ",'" + Convert.ToDateTime(DtOPBAL.Rows[0]["OPDATE"].ToString()).ToString(string.Format("yyyy/MM/dd")) + "',null,null,null,null,null,null,null,null,null,null,null,null,null,null,null," + Convert.ToDecimal(Openingvalue) + ",'" + Session["username"].ToString() + "',null,'OP',null,null,null,null,null,null,null,null,null;";
                                        strItemIssueResult += "set @IssueBatchID=(SELECT IDENT_CURRENT('IVR_ISSUEITEM_BATCHDETAILS') AS IssueBatchID);";
                                        #region for the value inserted into the CommonItem Issueretrun table
                                        //insert value into the IVR_COMMONITEM_ISSUERETURN Table DtIssueBatchItem
                                        //DtIssueBatchItem = DU.getdata(getIssueBatchItem);
                                        //if (DtIssueBatchItem.Rows.Count > 0)
                                        //{                                        
                                        strItemIssueResult += " Exec USP_IVR_COMMONITEM_ISSUERETURN " + Convert.ToInt32(DtOPBAL.Rows[0]["SLNUM"].ToString()) + ",@IssueID,@IssueDetailID,@IssueBatchID,null,null,null," + Convert.ToInt32(hidItemid.Value) + "," + Convert.ToInt32(BalanceQty) + "," + Convert.ToDouble(temprate) + ",'" + Convert.ToDateTime(DtOPBAL.Rows[0]["OPDATE"].ToString()).ToString(string.Format("yyyy/MM/dd")) + "',null,null,null,null,null,null,null,null,null,null,null,null," + Convert.ToDouble(Openingvalue) + ",'OP'," + Convert.ToDouble(txtslipno.Text) + "," + Convert.ToInt32(hidregid.Value) + "," + Convert.ToInt32(BalanceQty) + "," + Convert.ToDouble(Openingvalue) + ",'" + Convert.ToDateTime(txtIssuedDate.Text).ToString("yyyy/MM/dd") + "',null,null,null,null,null,null,null,null,null,null,null,null,null,null,null;";
                                        //}
                                        #endregion
                                        strItemIssueResult += " Update IVR_ITEMOPENBAL set ISSUEQTY=ISSUEQTY + " + BalanceQty + " where SLNUM=" + DtOPBAL.Rows[0]["SLNUM"].ToString() + ";";
                                        //DU.executesql(UpdateTable);
                                        Checkissueqty = BalanceQty;
                                    }
                                    //ISSUE from the Purchase table.
                                    #region Assign the Value for the variable
                                    double Purchasebalance = 0; double RemaningPurchaseQty = 0; double PurchaseRate = 0; double PurchaseValue = 0;
                                    double additionalrate = 0; double taxrate = 0; double taxvalue = 0; double extendedrate = 0; double extendedvalue = 0;
                                    double unitpriceinrrate = 0; double unitpricevalue = 0; double invoicerate = 0; double Invoicevalue = 0; double heducessoncvd = 0;
                                    double accessRate = 0; double Accessvalue = 0; double Cvdrate = 0; double cvdvalue = 0; double totaldutyrate = 0; double heducessoncvdvalue = 0;
                                    double Totaldutyvalue = 0; double additionalvalue = 0; double TotalPurchaseIssueQty = 0; double bcd = 0; double bcdvalue = 0;
                                    double taxper = 0; double freight = 0; double netamt = 0; double itmamt = 0; double packaging = 0; double educessoncvd = 0;
                                    double insurance = 0; double grandtotalduty = 0; double servicetaxamount = 0; double servicetax = 0; double educessoncvdvalue = 0;
                                    double taxpervalue = 0; double freightvalue = 0; double netamtvalue = 0; double itmamtvalue = 0; double packagingvalue = 0;
                                    double insurancevalue = 0; double grandtotaldutyvalue = 0; double servicetaxamountvalue = 0; double servicetaxvalue = 0;
                                    #endregion
                                    string SqlPurchase = "Select TITID,ISNULL(TAXPER,0) as TAXPER,ISNULL(HEDUCESSONCVD,0) as HEDUCESSONCVD,ISNULL(BASICCUSTOMDUTY,0) as BCD,ISNULL(EDUCESSONCVD,0) as EDUCESSONCVD,ISNULL(INSURANCE,0) as INSURANCE,ISNULL(PACKAGING,0) as PACKAGING,ISNULL(SERVICETAX,0) as SERVICETAX,ISNULL(SERVICETAXAMOUNT,0) as SERVICETAXAMOUNT,ISNULL(GRANDTOTALDUTY,0) as GRANDTOTALDUTY,ISNULL(ITMAMT,0) as ITMAMT,ISNULL(NETAMT,0) as NETAMT,ISNULL(FREIGHT,0) as FREIGHT,ITMID,ITMQTY,GROSSAMT,ISNULL(TAXAMT,0) as TAXAMT,ISNULL(BalanceQty,0) as BalanceQty,ISNULL(extendedvalue,0) as extendedvalue,ISNULL(UNITPRICEINR,Rate) as UNITPRICEINR,ISNULL(INVOICEVALUE,0)as INVOICEVALUE,ISNULL(ACCESSVALUE,0) as ACCESSVALUE,ISNULL(CVD,0) AS CVD,ISNULL(TOTALDUTY,0) AS TOTALDUTY,ISNULL(CUSTOMEDUCATIONAL,0) AS CUSTOMEDUCATIONAL,ISNULL(CUSTOMHEDU,0) AS CUSTOMHEDU,ISNULL(ADDDUTY,0) AS ADDDUTY,ENTRYTYPE,ReturnQty from IVR_TRAN_ITEM where itmid=" + hidItemid.Value;
                                    DataTable DtPurchase = DU.getdata(SqlPurchase);
                                    if (DtPurchase.Rows.Count > 0)
                                    {
                                        //Checkissueqty = 0;
                                        for (int p = 0; p < DtPurchase.Rows.Count; p++)
                                        {
                                            if (flag == 0)
                                            {
                                                #region loop
                                                Purchasebalance = Convert.ToDouble(DtPurchase.Rows[p]["BalanceQty"].ToString());
                                                if (Purchasebalance > 0)
                                                {
                                                    if (TempPurchaseQty > Purchasebalance)
                                                    {
                                                        RemaningPurchaseQty = TempPurchaseQty - Purchasebalance;
                                                        TotalPurchaseIssueQty = TempPurchaseQty - RemaningPurchaseQty;
                                                    }
                                                    if (TempPurchaseQty <= Purchasebalance)
                                                    {
                                                        TotalPurchaseIssueQty = TempPurchaseQty;
                                                        TempPurchaseQty = Purchasebalance - TempPurchaseQty;
                                                    }
                                                    if (TotalPurchaseIssueQty == 0)
                                                    {
                                                        break;
                                                    }
                                                    #region Calculate
                                                    //Purchase rate Calculation
                                                    if (Convert.ToDouble(DtPurchase.Rows[p]["GROSSAMT"].ToString()) > 0)
                                                    {
                                                        if (DtPurchase.Rows[p]["ENTRYTYPE"].ToString() == "RET")
                                                        {
                                                            PurchaseRate = Convert.ToDouble(DtPurchase.Rows[p]["GROSSAMT"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ReturnQty"].ToString());
                                                        }
                                                        else
                                                        {
                                                            PurchaseRate = Convert.ToDouble(DtPurchase.Rows[p]["GROSSAMT"]) / Convert.ToInt32(DtPurchase.Rows[p]["ITMQTY"]);
                                                        }
                                                        PurchaseValue = PurchaseRate * TotalPurchaseIssueQty;
                                                    }
                                                    //Purchase tax amt calculation
                                                    if (Convert.ToDouble(DtPurchase.Rows[p]["TAXAMT"].ToString()) > 0)
                                                    {
                                                        if (DtPurchase.Rows[p]["ENTRYTYPE"].ToString() == "RET")
                                                        {
                                                            taxrate = Convert.ToDouble(DtPurchase.Rows[p]["TAXAMT"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ReturnQty"].ToString());
                                                        }
                                                        else
                                                        {
                                                            taxrate = Convert.ToDouble(DtPurchase.Rows[p]["TAXAMT"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ITMQTY"]);
                                                        }
                                                        taxvalue = taxrate * TotalPurchaseIssueQty;
                                                    }
                                                    //purchase extendedvalue
                                                    if (Convert.ToDouble(DtPurchase.Rows[p]["extendedvalue"].ToString()) > 0)
                                                    {
                                                        if (DtPurchase.Rows[p]["ENTRYTYPE"].ToString() == "RET")
                                                        {
                                                            extendedrate = Convert.ToDouble(DtPurchase.Rows[p]["extendedvalue"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ReturnQty"]);
                                                        }
                                                        else
                                                        {
                                                            extendedrate = Convert.ToDouble(DtPurchase.Rows[p]["extendedvalue"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ITMQTY"]);
                                                        }
                                                        extendedvalue = extendedrate * TotalPurchaseIssueQty;
                                                    }
                                                    //purchase unitpriceinr
                                                    if (Convert.ToDouble(DtPurchase.Rows[p]["UNITPRICEINR"].ToString()) > 0)
                                                    {
                                                        if (DtPurchase.Rows[p]["ENTRYTYPE"].ToString() == "RET")
                                                        {
                                                            unitpriceinrrate = Convert.ToDouble(DtPurchase.Rows[p]["UNITPRICEINR"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ReturnQty"].ToString());
                                                        }
                                                        else
                                                        {
                                                            unitpriceinrrate = Convert.ToDouble(DtPurchase.Rows[p]["UNITPRICEINR"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ITMQTY"]);
                                                        }
                                                        unitpricevalue = unitpriceinrrate * TotalPurchaseIssueQty;
                                                    }
                                                    //purchase invoicevalue
                                                    if (Convert.ToDouble(DtPurchase.Rows[p]["INVOICEVALUE"].ToString()) > 0)
                                                    {
                                                        if (DtPurchase.Rows[p]["ENTRYTYPE"].ToString() == "RET")
                                                        {
                                                            invoicerate = Convert.ToDouble(DtPurchase.Rows[p]["INVOICEVALUE"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ReturnQty"].ToString());
                                                        }
                                                        else
                                                        {
                                                            invoicerate = Convert.ToDouble(DtPurchase.Rows[p]["INVOICEVALUE"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ITMQTY"]);
                                                        }
                                                        Invoicevalue = invoicerate * TotalPurchaseIssueQty;
                                                    }
                                                    //Purchase accessvalue
                                                    if (Convert.ToDouble(DtPurchase.Rows[p]["ACCESSVALUE"].ToString()) > 0)
                                                    {
                                                        if (DtPurchase.Rows[p]["ENTRYTYPE"].ToString() == "RET")
                                                        {
                                                            accessRate = Convert.ToDouble(DtPurchase.Rows[p]["ACCESSVALUE"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ReturnQty"].ToString());
                                                        }
                                                        else
                                                        {
                                                            accessRate = Convert.ToDouble(DtPurchase.Rows[p]["ACCESSVALUE"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ITMQTY"]);
                                                        }
                                                        Accessvalue = accessRate * TotalPurchaseIssueQty;
                                                    }
                                                    //purchase cvd
                                                    if (Convert.ToDouble(DtPurchase.Rows[p]["CVD"].ToString()) > 0)
                                                    {
                                                        if (DtPurchase.Rows[p]["ENTRYTYPE"].ToString() == "RET")
                                                        {
                                                            Cvdrate = Convert.ToDouble(DtPurchase.Rows[p]["CVD"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ReturnQty"].ToString());
                                                        }
                                                        else
                                                        {
                                                            Cvdrate = Convert.ToDouble(DtPurchase.Rows[p]["CVD"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ITMQTY"]);
                                                        }
                                                        cvdvalue = Cvdrate * TotalPurchaseIssueQty;
                                                    }

                                                    if (Convert.ToDouble(DtPurchase.Rows[p]["BCD"].ToString()) > 0)
                                                    {
                                                        if (DtPurchase.Rows[p]["ENTRYTYPE"].ToString() == "RET")
                                                        {
                                                            bcd = Convert.ToDouble(DtPurchase.Rows[p]["BCD"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ReturnQty"].ToString());
                                                        }
                                                        else
                                                        {
                                                            bcd = Convert.ToDouble(DtPurchase.Rows[p]["BCD"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ITMQTY"]);
                                                        }
                                                        bcdvalue = bcd * TotalPurchaseIssueQty;
                                                    }
                                                    if (Convert.ToDouble(DtPurchase.Rows[p]["EDUCESSONCVD"].ToString()) > 0)
                                                    {
                                                        if (DtPurchase.Rows[p]["ENTRYTYPE"].ToString() == "RET")
                                                        {
                                                            educessoncvd = Convert.ToDouble(DtPurchase.Rows[p]["EDUCESSONCVD"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ReturnQty"].ToString());
                                                        }
                                                        else
                                                        {
                                                            educessoncvd = Convert.ToDouble(DtPurchase.Rows[p]["EDUCESSONCVD"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ITMQTY"]);
                                                        }
                                                        educessoncvdvalue = educessoncvd * TotalPurchaseIssueQty;
                                                    }
                                                    if (Convert.ToDouble(DtPurchase.Rows[p]["HEDUCESSONCVD"].ToString()) > 0)
                                                    {
                                                        if (DtPurchase.Rows[p]["ENTRYTYPE"].ToString() == "RET")
                                                        {
                                                            heducessoncvd = Convert.ToDouble(DtPurchase.Rows[p]["HEDUCESSONCVD"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ReturnQty"].ToString());
                                                        }
                                                        else
                                                        {
                                                            heducessoncvd = Convert.ToDouble(DtPurchase.Rows[p]["HEDUCESSONCVD"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ITMQTY"]);
                                                        }
                                                        heducessoncvdvalue = heducessoncvd * TotalPurchaseIssueQty;
                                                    }


                                                    //purchase totalduty
                                                    if (Convert.ToDouble(DtPurchase.Rows[p]["TOTALDUTY"].ToString()) > 0)
                                                    {
                                                        if (DtPurchase.Rows[p]["ENTRYTYPE"].ToString() == "RET")
                                                        {
                                                            totaldutyrate = Convert.ToDouble(DtPurchase.Rows[p]["TOTALDUTY"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ReturnQty"].ToString());
                                                        }
                                                        else
                                                        {
                                                            totaldutyrate = Convert.ToDouble(DtPurchase.Rows[p]["TOTALDUTY"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ITMQTY"]);
                                                        }
                                                        Totaldutyvalue = totaldutyrate * TotalPurchaseIssueQty;
                                                    }
                                                    //purchase additionalduty
                                                    if (Convert.ToDouble(DtPurchase.Rows[p]["ADDDUTY"].ToString()) > 0)
                                                    {
                                                        if (DtPurchase.Rows[p]["ENTRYTYPE"].ToString() == "RET")
                                                        {
                                                            additionalrate = Convert.ToDouble(DtPurchase.Rows[p]["ADDDUTY"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ReturnQty"].ToString());
                                                        }
                                                        else
                                                        {
                                                            additionalrate = Convert.ToDouble(DtPurchase.Rows[p]["ADDDUTY"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ITMQTY"]);
                                                        }
                                                        additionalvalue = additionalrate * TotalPurchaseIssueQty;
                                                    }

                                                    if (Convert.ToDouble(DtPurchase.Rows[p]["TAXPER"].ToString()) > 0)
                                                    {
                                                        if (DtPurchase.Rows[p]["ENTRYTYPE"].ToString() == "RET")
                                                        {
                                                            taxper = Convert.ToDouble(DtPurchase.Rows[p]["TAXPER"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ReturnQty"].ToString());
                                                        }
                                                        else
                                                        {
                                                            taxper = Convert.ToDouble(DtPurchase.Rows[p]["TAXPER"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ITMQTY"]);
                                                        }
                                                        taxpervalue = taxper * TotalPurchaseIssueQty;
                                                    }
                                                    if (Convert.ToDouble(DtPurchase.Rows[p]["INSURANCE"].ToString()) > 0)
                                                    {
                                                        if (DtPurchase.Rows[p]["ENTRYTYPE"].ToString() == "RET")
                                                        {
                                                            insurance = Convert.ToDouble(DtPurchase.Rows[p]["INSURANCE"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ReturnQty"].ToString());
                                                        }
                                                        else
                                                        {
                                                            insurance = Convert.ToDouble(DtPurchase.Rows[p]["INSURANCE"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ITMQTY"]);
                                                        }
                                                        insurancevalue = insurance * TotalPurchaseIssueQty;
                                                    }
                                                    if (Convert.ToDouble(DtPurchase.Rows[p]["PACKAGING"].ToString()) > 0)
                                                    {
                                                        if (DtPurchase.Rows[p]["ENTRYTYPE"].ToString() == "RET")
                                                        {
                                                            packaging = Convert.ToDouble(DtPurchase.Rows[p]["PACKAGING"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ReturnQty"].ToString());
                                                        }
                                                        else
                                                        {
                                                            packaging = Convert.ToDouble(DtPurchase.Rows[p]["PACKAGING"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ITMQTY"]);
                                                        }
                                                        packagingvalue = packaging * TotalPurchaseIssueQty;
                                                    }
                                                    if (Convert.ToDouble(DtPurchase.Rows[p]["SERVICETAX"].ToString()) > 0)
                                                    {
                                                        if (DtPurchase.Rows[p]["ENTRYTYPE"].ToString() == "RET")
                                                        {
                                                            servicetax = Convert.ToDouble(DtPurchase.Rows[p]["SERVICETAX"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ReturnQty"].ToString());
                                                        }
                                                        else
                                                        {
                                                            servicetax = Convert.ToDouble(DtPurchase.Rows[p]["SERVICETAX"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ITMQTY"]);
                                                        }
                                                        servicetaxvalue = servicetax * TotalPurchaseIssueQty;
                                                    }
                                                    if (Convert.ToDouble(DtPurchase.Rows[p]["SERVICETAXAMOUNT"].ToString()) > 0)
                                                    {
                                                        if (DtPurchase.Rows[p]["ENTRYTYPE"].ToString() == "RET")
                                                        {
                                                            servicetaxamount = Convert.ToDouble(DtPurchase.Rows[p]["SERVICETAXAMOUNT"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ReturnQty"].ToString());
                                                        }
                                                        else
                                                        {
                                                            servicetaxamount = Convert.ToDouble(DtPurchase.Rows[p]["SERVICETAXAMOUNT"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ITMQTY"]);
                                                        }
                                                        servicetaxamountvalue = servicetaxamount * TotalPurchaseIssueQty;
                                                    }
                                                    if (Convert.ToDouble(DtPurchase.Rows[p]["GRANDTOTALDUTY"].ToString()) > 0)
                                                    {
                                                        if (DtPurchase.Rows[p]["ENTRYTYPE"].ToString() == "RET")
                                                        {
                                                            grandtotalduty = Convert.ToDouble(DtPurchase.Rows[p]["GRANDTOTALDUTY"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ReturnQty"].ToString());
                                                        }
                                                        else
                                                        {
                                                            grandtotalduty = Convert.ToDouble(DtPurchase.Rows[p]["GRANDTOTALDUTY"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ITMQTY"]);
                                                        }
                                                        grandtotaldutyvalue = grandtotalduty * TotalPurchaseIssueQty;
                                                    }
                                                    if (Convert.ToDouble(DtPurchase.Rows[p]["ITMAMT"].ToString()) > 0)
                                                    {
                                                        if (DtPurchase.Rows[p]["ENTRYTYPE"].ToString() == "RET")
                                                        {
                                                            itmamt = Convert.ToDouble(DtPurchase.Rows[p]["ITMAMT"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ReturnQty"].ToString());
                                                        }
                                                        else
                                                        {
                                                            itmamt = Convert.ToDouble(DtPurchase.Rows[p]["ITMAMT"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ITMQTY"]);
                                                        }
                                                        itmamtvalue = itmamt * TotalPurchaseIssueQty;
                                                    }
                                                    if (Convert.ToDouble(DtPurchase.Rows[p]["NETAMT"].ToString()) > 0)
                                                    {
                                                        if (DtPurchase.Rows[p]["ENTRYTYPE"].ToString() == "RET")
                                                        {
                                                            netamt = Convert.ToDouble(DtPurchase.Rows[p]["NETAMT"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ReturnQty"].ToString());
                                                        }
                                                        else
                                                        {
                                                            netamt = Convert.ToDouble(DtPurchase.Rows[p]["NETAMT"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ITMQTY"]);
                                                        }
                                                        netamtvalue = netamt * TotalPurchaseIssueQty;
                                                    }
                                                    if (Convert.ToDouble(DtPurchase.Rows[p]["FREIGHT"].ToString()) > 0)
                                                    {
                                                        if (DtPurchase.Rows[p]["ENTRYTYPE"].ToString() == "RET")
                                                        {
                                                            freight = Convert.ToDouble(DtPurchase.Rows[p]["FREIGHT"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ReturnQty"].ToString());
                                                        }
                                                        else
                                                        {
                                                            freight = Convert.ToDouble(DtPurchase.Rows[p]["FREIGHT"].ToString()) / Convert.ToInt32(DtPurchase.Rows[p]["ITMQTY"]);
                                                        }
                                                        freightvalue = freight * TotalPurchaseIssueQty;
                                                    }
                                                    #endregion
                                                    strItemIssueResult += " Exec USP_IVR_ISSUEITEM_BATCHDETAILS @IssueDetailID,null," + Convert.ToInt32(hidItemid.Value) + ",null,null,null," + Convert.ToInt32(DtPurchase.Rows[p]["TITID"].ToString()) + "," + Convert.ToInt32(TotalPurchaseIssueQty) + "," + Convert.ToDouble(taxvalue) + "," + Convert.ToDouble(extendedvalue) + "," + Convert.ToDouble(unitpriceinrrate) + "," + Convert.ToDouble(Invoicevalue) + "," + Convert.ToDouble(Accessvalue) + "," + Convert.ToDouble(bcdvalue) + "," + Convert.ToDouble(cvdvalue) + "," + Convert.ToDouble(educessoncvdvalue) + "," + Convert.ToDouble(heducessoncvdvalue) + "," + Convert.ToDouble(Totaldutyvalue) + ",null,null," + Convert.ToDouble(additionalvalue) + "," + Convert.ToDouble(PurchaseValue) + ",'" + Session["username"].ToString() + "'," + Convert.ToDouble(PurchaseRate) + ",'PUR'," + Convert.ToDouble(taxpervalue) + "," + Convert.ToDouble(freightvalue) + "," + Convert.ToDouble(netamtvalue) + "," + Convert.ToDouble(itmamtvalue) + "," + Convert.ToDouble(insurancevalue) + "," + Convert.ToDouble(packagingvalue) + "," + Convert.ToDouble(grandtotaldutyvalue) + "," + Convert.ToDouble(servicetaxamountvalue) + "," + Convert.ToDouble(servicetaxvalue) + ";";
                                                    strItemIssueResult += " set @IssueBatchID=(SELECT IDENT_CURRENT('IVR_ISSUEITEM_BATCHDETAILS') AS IssueBatchID); ";
                                                    //insert value into the IVR_COMMONITEM_ISSUERETURN Table                                                    
                                                    //DataTable DtIssuePurchaseBatchItem = DU.getdata(getIssuePurchaseBatchItem);
                                                    //if (DtIssuePurchaseBatchItem.Rows.Count > 0)
                                                    //{
                                                    strItemIssueResult += " Exec USP_IVR_COMMONITEM_ISSUERETURN null,@IssueID,@IssueDetailID,@IssueBatchID,null,null,null," + Convert.ToInt32(hidItemid.Value) + ",null,null,null," + Convert.ToInt32(DtPurchase.Rows[p]["TITID"].ToString()) + "," + Convert.ToInt32(TotalPurchaseIssueQty) + "," + Convert.ToDouble(taxvalue) + "," + Convert.ToDouble(extendedvalue) + "," + Convert.ToDouble(unitpriceinrrate) + "," + Convert.ToDouble(Invoicevalue) + "," + Convert.ToDouble(Accessvalue) + "," + Convert.ToDouble(cvdvalue) + "," + Convert.ToDouble(Totaldutyvalue) + ",null,null," + Convert.ToDouble(additionalvalue) + "," + Convert.ToDouble(PurchaseValue) + ",'PUR'," + Convert.ToInt32(txtslipno.Text) + "," + Convert.ToInt32(hidregid.Value) + "," + Convert.ToInt32(TotalPurchaseIssueQty) + "," + Convert.ToDouble(PurchaseValue) + ",'" + Convert.ToDateTime(txtIssuedDate.Text).ToString(string.Format("yyyy/MM/dd")) + "',null,null,null," + Convert.ToDouble(taxpervalue) + "," + Convert.ToDouble(freightvalue) + "," + Convert.ToDouble(netamtvalue) + "," + Convert.ToDouble(itmamtvalue) + "," + Convert.ToDouble(insurancevalue) + "," + Convert.ToDouble(packagingvalue) + "," + Convert.ToDouble(grandtotaldutyvalue) + "," + Convert.ToDouble(servicetaxamountvalue) + "," + Convert.ToDouble(servicetaxvalue) + "," + Convert.ToDouble(bcdvalue) + "," + Convert.ToDouble(educessoncvdvalue) + "," + Convert.ToDouble(heducessoncvdvalue) + ";";
                                                    //}                                                
                                                    TempPurchaseQty = RemaningPurchaseQty;
                                                    Checkissueqty += TotalPurchaseIssueQty;
                                                    strItemIssueResult += " Update IVR_TRAN_ITEM set IssueQty=ISNULL(IssueQty,0) +" + Convert.ToInt32(TotalPurchaseIssueQty) + " where TITID=" + Convert.ToInt32(DtPurchase.Rows[p]["TITID"].ToString()) + ";";
                                                    //DU.executesql(UpdatePurchaseTable);
                                                }
                                                #endregion
                                                if (Convert.ToDouble(txtIssueQty.Text) == Checkissueqty)
                                                {
                                                    strItemIssueResult += " update IVR_OnlineRegistration_Detail set IssueQty=ISNULL(IssueQty,0) +" + Convert.ToInt32(txtIssueQty.Text) + ",IssuedDate='" + Convert.ToDateTime(txtIssuedDate.Text).ToString("yyyy-MM-dd") + "',ITMID=" + hidItemid.Value + " where Id=" + Convert.ToInt32(Hid.Value) + ";";
                                                    //int resultissue = DU.executesql(sqlIssue);
                                                    flag = 1;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            #endregion
                            if (flag == 0)
                            {
                                if (Convert.ToDouble(txtIssueQty.Text) == Checkissueqty)
                                {
                                    strItemIssueResult += " update IVR_OnlineRegistration_Detail set IssueQty=ISNULL(IssueQty,0) +'" + txtIssueQty.Text + "',IssuedDate='" + Convert.ToDateTime(txtIssuedDate.Text).ToString("yyyy-MM-dd") + "',ITMID=" + hidItemid.Value + " where Id='" + Hid.Value + "';";
                                    //int resultissue = DU.executesql(sqlIssue);
                                }
                            }
                            #endregion
                        }
                        //}
                        #endregion
                    }
                    #endregion
                    //For The Transaction Completed.
                    int Result = DU.TranExecuteSql(strItemIssueResult);
                    string slipno = txtslipno.Text;
                    txtslipno.Text = "";
                    ClearTable();
                    //for reload the Page.
                    Response.Redirect("IssueItem.aspx?mode=Save&SlipNo=" + slipno);
                }
            }
            else
            {
                lblcaptchaerror.Text = "InCorrect Answer.... Please try again.";
                lblcaptchaerror.ForeColor = System.Drawing.Color.Red;
                lblcaptchaerror.Visible = true;
            }
        }
        catch (Exception ex)
        {
            lblmsg.Text = ex.Message;
            lblmsg.Visible = true;
        }        
    }
    protected void BindUsername()
    {
        string sqlusername = "select (ISNULL(FirstName,'') +' '+ ISNULL(lastName,'')) as USERNAME from Login where UserID='" + Session["userid"].ToString() + "'";
        DataTable dt = DU.getdata(sqlusername);
        if (dt.Rows.Count > 0)
        {
            txtissuedby.Text = dt.Rows[0]["USERNAME"].ToString().ToUpper();
            //txtreqdate.Text = System.DateTime.Now.ToShortDateString();            
        }
    }    
    protected void txtslipno_TextChanged(object sender, EventArgs e)
    {
        string sql = "select slipno from IVR_OnlineRegistrationSlip where slipno='" + txtslipno.Text + "'";
        DataTable dt = DU.getdata(sql);
        if (dt.Rows.Count <= 0)
        {
            txtslipno.Text = "";
            pnldata.Visible = false;
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        pnldata.Visible = false;
        txtslipno.Text = "";
    }
    protected void grdview_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView GrdIssueChild = (GridView)e.Row.FindControl("gvIssuItemChild");
            string SlipNo = grdview.DataKeys[e.Row.RowIndex].Value.ToString();
            if (SlipNo != "")
            {
                SqlParameter[] param = { new SqlParameter("@issueid", SlipNo) };
                DataTable DtIssueItem = DU.getdatausingprocedure("USP_IVR_IssueItemView", param);
                DataTable DTChildIssueItem = new DataTable();
                DTChildIssueItem.Columns.Add("ItemCode", typeof(string));
                DTChildIssueItem.Columns.Add("ItemDesc", typeof(string));
                DTChildIssueItem.Columns.Add("RO", typeof(string));
                DTChildIssueItem.Columns.Add("CustomerName", typeof(string));
                DTChildIssueItem.Columns.Add("projectname", typeof(string));
                DTChildIssueItem.Columns.Add("RequestQTY", typeof(string));
                DTChildIssueItem.Columns.Add("IssueQty", typeof(string));
                if (DtIssueItem.Rows.Count > 0)
                {
                    for (int i = 0; i < DtIssueItem.Rows.Count; i++)
                    {
                        DataRow DR = DTChildIssueItem.NewRow();
                        DR["ItemCode"] = DtIssueItem.Rows[i]["ITMCODE"].ToString();
                        DR["ItemDesc"] = DtIssueItem.Rows[i]["ITEMDESC"].ToString();
                        DR["RO"] = DtIssueItem.Rows[i]["RO"].ToString();
                        DR["CustomerName"] = DtIssueItem.Rows[i]["CustomerName"].ToString();
                        DR["projectname"] = DtIssueItem.Rows[i]["projectname"].ToString();
                        DR["RequestQTY"] = DtIssueItem.Rows[i]["RequestQTY"].ToString();
                        DR["IssueQty"] = DtIssueItem.Rows[i]["IssueQty"].ToString();

                        DTChildIssueItem.Rows.Add(DR);
                    }
                }
                GrdIssueChild.DataSource = DTChildIssueItem;
                GrdIssueChild.DataBind();
            }
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        try
        {
            GrdViewIssuedItem();
            dtDetail.DefaultView.RowFilter = "SLIPNO like '%" + txtslipnoSearch.Text+"%'";
            if (dtDetail.Rows.Count > 0)
            {
                grdview.DataSource = dtDetail;
                grdview.DataBind();
            }
            else
            {
                lblmsg.Text = "No Record Found !";
                lblmsg.ForeColor = System.Drawing.Color.Green;
                lblmsg.Attributes.Add("style", "display:block");
            }
        }
        catch (Exception Ex)
        {
        }
    }
}